package dataConstructions_finished;

public class T566 {
    public int[][] matrixReshape(int[][] mat, int r, int c) {
        int m = mat.length;
        int n = mat[0].length;
        if (m*n != r*c) {
            return mat;
        }

        int ir = 0, ic = 0;
        int[][] ret = new int[r][c];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (ic == c) {
                    ic = 0;
                    ir++;
                }
                ret[ir][ic] = mat[i][j];
                ic++;
            }
        }

        return ret;
    }
}
